﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using SRM.Entities;
using System.Collections;

namespace SRM
{
    public partial class TestTelerikManual : System.Web.UI.Page
    {
        const string _applicantKey = "6814";

        FamilyMember selectedFamilyMember = new FamilyMember();

        System.Data.DataSet ds = new System.Data.DataSet("Page_DataSet");
        System.Data.DataTable dtApplicant = null;
        System.Data.DataRow drApplicant = null;
        bool Use_SAP = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            dtApplicant = Help.GetApplicant(_applicantKey);

            if ((dtApplicant != null) && (dtApplicant.Rows.Count > 0))
            {
                drApplicant = dtApplicant.Rows[0];
                Use_SAP = Help.GetIsSapRecord(drApplicant);
            }

            FamilyParticularsRadGrid.DataSource = Help.GetFamilyMember(_applicantKey);

            
        }

        private void SetDateOfMarriageVisibility(string relationship)
        {
            if (relationship.Equals("Spouse", StringComparison.InvariantCultureIgnoreCase))
            {
                GridDateTimeColumn gtc = FamilyParticularsRadGrid.MasterTableView.GetColumn("DateOfMarriageColumn") as GridDateTimeColumn;
                gtc.ReadOnly = false;
            }
            else
            {
                GridDateTimeColumn gtc = FamilyParticularsRadGrid.MasterTableView.GetColumn("DateOfMarriageColumn") as GridDateTimeColumn;
                gtc.ReadOnly = true;
            }
        }

        protected void FamilyParticularsRadGrid_EditCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            System.Diagnostics.Debugger.Break();
            
            this.selectedFamilyMember = ((List<FamilyMember>)FamilyParticularsRadGrid.DataSource)[e.Item.DataSetIndex] as FamilyMember;
            this.SetDateOfMarriageVisibility(selectedFamilyMember.Relationship);
        }

        protected void FamilyParticularsRadGrid_UpdateCommand(object source, GridCommandEventArgs e)
        {
            // Add code to update record in SRM_ApplicantFamilyMembers_Tbl
            System.Diagnostics.Debugger.Break();
            FamilyMember updateFamilyMember = ((List<FamilyMember>)FamilyParticularsRadGrid.DataSource)[e.Item.DataSetIndex] as FamilyMember;

            if (updateFamilyMember != null)
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
                Hashtable newValues = new Hashtable();
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                RadDatePicker DateOfMarriageRadDatePicker = editedItem.FindControl("DateOfMarriageRadDatePicker") as RadDatePicker;
                DropDownList RelationshipDropDownList = editedItem.FindControl("RelationshipDropDownList") as DropDownList;

                //["MemberName"]: "Ong Li Ling"
                //["BirthDate"]: null
                //["Nationality"]: "SG"
                //["Occupation"]: null
                //["FamilyEmployer"]: null
                //["EmergencyContact"]: null
                //["ContactNo"]: "12312313"

                updateFamilyMember.MemberName = Help.ParsedObjectToGetString(newValues["MemberName"]);
                updateFamilyMember.BirthDate = Help.ParsedObjectToGetNullDateTime(newValues["BirthDate"]);
                updateFamilyMember.DateOfMarriage = Help.ParsedObjectToGetNullDateTime(newValues["DateOfMarriage"]);
                updateFamilyMember.Nationality = Help.ParsedObjectToGetString(newValues["Nationality"]);
                updateFamilyMember.Occupation = Help.ParsedObjectToGetString(newValues["Occupation"]);
                updateFamilyMember.FamilyEmployer = Help.ParsedObjectToGetString(newValues["FamilyEmployer"]);
                updateFamilyMember.EmergencyContact = Help.ParsedObjectToGetString(newValues["EmergencyContact"]);
                updateFamilyMember.ContactNo = Help.ParsedObjectToGetString(newValues["ContactNo"]);

                
            }
        }

        protected void FamilyParticularsRadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            FamilyMember familyMember = e.Item.DataItem as FamilyMember;
            
            if (e.Item.IsInEditMode)
            {
                //GridEditFormItem item = (GridEditFormItem)e.Item;
                //DropDownList RelationshipDropDownList = item.FindControl("RelationshipDropDownList") as DropDownList;
                //RelationshipDropDownList.DataSource = Help.GetSRMCode("REL", false);
                //RelationshipDropDownList.SelectedValue = selectedFamilyMember.Relationship;
                //RelationshipDropDownList.DataBind();
            }
        }

        protected void RelationshipDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.selectedFamilyMember = new FamilyMember();
            this.selectedFamilyMember.Relationship = ((DropDownList)sender).SelectedValue;
            this.SetDateOfMarriageVisibility(selectedFamilyMember.Relationship);
            this.FamilyParticularsRadGrid.Rebind();
        }

        protected void FamilyParticularsRadGrid_InsertCommand(object source, GridCommandEventArgs e)
        {
            System.Diagnostics.Debugger.Break();
        }

        protected void FamilyParticularsRadGrid_DeleteCommand(object source, GridCommandEventArgs e)
        {
            System.Diagnostics.Debugger.Break();
        }
        
    }
}